<template>
  <div>
    <el-dialog
      :title="isUpdate ? '编辑' : '新建'"
      v-model="props.visible"
      width="50%"
      @close="closeDialog()"
    >
      <el-form
        :model="form"
        label-position="right"
        label-width="100px"
        ref="formRef"
        @submit.prevent
        :rules="rules"
      >
        <el-row :gutter="20">
          <el-col :span="17" :offset="1">
            <el-form-item label="连接名称:" prop="connectName">
              <el-input
                placeholder="请输入连接名称"
                v-model="form.connectName"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="17" :offset="1">
            <el-form-item label="数据库类型:" prop="dbType">
              <el-select v-model="form.dbType">
                <el-option
                  v-for="item in dbTypeList"
                  :label="item"
                  :value="item"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="17" :offset="1">
            <el-form-item label="连接地址：" prop="dbIp">
              <el-input
                placeholder="只输入ip即可"
                v-model="form.dbIp"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="17" :offset="1">
            <el-form-item label="用户名:" prop="dbUser">
              <el-input
                placeholder="请输入数据库用户名"
                v-model="form.dbUser"
                :rows="6"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="17" :offset="1">
            <el-form-item label="密码：" prop="dbPassword">
              <el-input
                placeholder="请输入数据库密码"
                v-model="form.dbPassword"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="17" :offset="1">
            <el-form-item label="端口号： " prop="dbPort">
              <el-input
                placeholder="请输入数据库端口"
                v-model="form.dbPort"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="17" :offset="1">
            <el-form-item label="库名： " prop="dbName">
              <el-input
                placeholder="请输入使用的库名"
                v-model="form.dbName"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="18" :offset="1">
            <el-form-item label="是否启用：" prop="isOpen">
              <el-switch
                v-model="form.isOpen"
                inline-prompt
                active-text="启用"
                inactive-text="关闭"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="17" :offset="1">
            <el-form-item label="备注：" prop="remark">
              <el-input
                placeholder=""
                type="textarea"
                v-model="form.remark"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-row :gutter="20"
          ><el-col :span="17" :offset="1">
            <div
              class="dialog-footer"
              style="display: flex; justify-content: left; margin-bottom: 30px"
            >
              <el-button type="primary" @click="save()">确 定</el-button>
              <el-button @click="closeDialog()">取 消</el-button>
            </div>
          </el-col>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { tkDbConfigApi } from "@/api/kernel/tk-db-config.js";

const dbTypeList = ref(["Mysql", "Mongo"]);

const { proxy } = getCurrentInstance();

const formRef = ref(null);
const props = defineProps({
  visible: Boolean,
  data: Object,
});
const emits = defineEmits(["update:visible", "done"]);

const form = ref({
  isOpen: true,
});
const rules = ref({
  connectName: [
    { required: true, message: "连接名称不能为空", trigger: "blur" },
  ],
  dbType: [{ required: true, message: "请选择数据库类型", trigger: "blur" }],
  dbIP: [{ required: true, message: "连接地址ip不能为空", trigger: "blur" }],
  dbUser: [
    { required: true, message: "数据库用户名不能为空", trigger: "blur" },
  ],
  dbPassword: [
    { required: true, message: "数据库密码不能为空", trigger: "blur" },
  ],
  dbPort: [{ required: true, message: "数据库端口不能为空", trigger: "blur" }],
  dbName: [{ required: true, message: "请输入库名", trigger: "blur" }],
});
onMounted(async () => {
  if (props.data) {
    console.log("修改===========");
    getInfoById(props.data);
    isUpdate.value = true;
  } else {
    console.log("添加=============");
    isUpdate.value = false;
  }
});
const getInfoById = (data) => {
  let { id } = data;
  tkDbConfigApi.getInfoById(id).then((res) => {
    form.value = Object.assign({}, res.data);
  });
};

// 是否是编辑状态
const isUpdate = ref(false);

const closeDialog = () => {
  formRef.value.resetFields();
  updateVisible(false);
  emits("done");
};
// 更改弹框状态
const updateVisible = (value) => {
  emits("update:visible", value);
};

//#- 保存
const loading = ref(false);

const save = () => {
  proxy.$refs["formRef"].validate((valid) => {
    console.log(form.value);
    if (valid) {
      loading.value = true;
      if (isUpdate.value) {
        tkDbConfigApi.edit(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
        });
      } else {
        tkDbConfigApi.add(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          console.log("add:response", response);
        });
      }

      loading.value = false;
      closeDialog();
    }
  });
};
</script>
